package com.geekai.chat.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.geekai.chat.config.AiConfig;

@Service
public class ModelSelectionService {

    private final AiConfig aiConfig;

    public ModelSelectionService(AiConfig aiConfig) {
        this.aiConfig = aiConfig;
    }

    /**
     * 获取所有可用的模型列表
     */
    public List<ModelInfo> getAvailableModels() {
        return aiConfig.getModels().entrySet().stream()
                .map(entry -> {
                    ModelInfo modelInfo = new ModelInfo();
                    modelInfo.setType(entry.getKey());
                    modelInfo.setName(entry.getValue().getModelName());
                    modelInfo.setDisplayName(getDisplayName(entry.getKey()));
                    modelInfo.setDescription(getDescription(entry.getKey()));
                    return modelInfo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 验证模型是否可用
     */
    public boolean isModelAvailable(String modelType) {
        return aiConfig.getModels().containsKey(modelType);
    }

    /**
     * 获取模型配置
     */
    public AiConfig.ModelConfig getModelConfig(String modelType) {
        return aiConfig.getModelConfig(modelType);
    }

    private String getDisplayName(String modelType) {
        switch (modelType.toLowerCase()) {
            case "gpt":
                return "GPT (OpenAI)";
            case "deepseek":
                return "DeepSeek";
            default:
                return modelType;
        }
    }

    private String getDescription(String modelType) {
        switch (modelType.toLowerCase()) {
            case "gpt":
                return "OpenAI 的 GPT 系列模型，擅长自然语言理解和生成";
            case "deepseek":
                return "DeepSeek 的对话模型，支持中文对话";
            default:
                return "AI 对话模型";
        }
    }

    public static class ModelInfo {
        private String type;
        private String name;
        private String displayName;
        private String description;

        // Getters and Setters
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}
